# Check header files
CHECK_INCLUDE_FILES(arpa/inet.h HAVE_ARPA_INET_H)
CHECK_INCLUDE_FILES(fcntl.h HAVE_FCNTL_H)
CHECK_INCLUDE_FILES(float.h HAVE_FLOAT_H)
CHECK_INCLUDE_FILES(inttypes.h HAVE_INTTYPES_H)
CHECK_INCLUDE_FILES(limits.h HAVE_LIMITS_H)
CHECK_INCLUDE_FILES(malloc.h HAVE_MALLOC_H)
CHECK_INCLUDE_FILES(math.h HAVE_MATH_H)
CHECK_INCLUDE_FILES(memory.h HAVE_MEMORY_H)
CHECK_INCLUDE_FILES(netdb.h HAVE_NETDB_H)
CHECK_INCLUDE_FILES(netinet/in.h HAVE_NETINET_IN_H)
CHECK_INCLUDE_FILES(sched.h HAVE_SCHED_H)
CHECK_INCLUDE_FILES(stddef.h HAVE_STDDEF_H)
CHECK_INCLUDE_FILES(stdint.h HAVE_STDINT_H)
CHECK_INCLUDE_FILES(stdlib.h HAVE_STDLIB_H)
CHECK_INCLUDE_FILES(string.h HAVE_STRING_H)
CHECK_INCLUDE_FILES(strings.h HAVE_STRINGS_H)
CHECK_INCLUDE_FILES(sys/ioctl.h HAVE_IOCTL_H)
CHECK_INCLUDE_FILES(sys/param.h HAVE_PARAM_H)
CHECK_INCLUDE_FILES(sys/socket.h HAVE_SOCKET_H)
CHECK_INCLUDE_FILES(sys/timeb.h HAVE_TIMEB_H)
CHECK_INCLUDE_FILES(sys/time.h HAVE_TIME_H)
CHECK_INCLUDE_FILES(unistd.h HAVE_UNISTD_H)
CHECK_INCLUDE_FILES(wchar.h HAVE_WCHAR_H)
CHECK_INCLUDE_FILES(curses.h HAVE_CURSES_H)
CHECK_INCLUDE_FILES(ncursesw/curses.h HAVE_NCURSESW_CURSES_H)
CHECK_INCLUDE_FILES(ncursesw.h HAVE_NCURSESW_H)
CHECK_INCLUDE_FILES(ncurses/curses.h HAVE_NCURSES_CURSES_H)
CHECK_INCLUDE_FILES(ncurses.h HAVE_NCURSES_H)
CHECK_INCLUDE_FILES(dlfcn.h HAVE_DLFCN_H)
CHECK_INCLUDE_FILES(sys/ioctl.h HAVE_SYS_IOCTL_H)
CHECK_INCLUDE_FILES(sys/param.h HAVE_SYS_PARAM_H)
CHECK_INCLUDE_FILES(sys/socket.h HAVE_SYS_SOCKET_H)
CHECK_INCLUDE_FILES(sys/stat.h HAVE_SYS_STAT_H)
CHECK_INCLUDE_FILES(sys/timeb.h HAVE_SYS_TIMEB_H)
CHECK_INCLUDE_FILES(sys/time.h HAVE_SYS_TIME_H)
CHECK_INCLUDE_FILES(sys/types.h HAVE_SYS_TYPES_H)

CHECK_FUNCTION_EXISTS(madvise HAVE_MADVISE)
CHECK_FUNCTION_EXISTS(error_at_line HAVE_ERROR_AT_LINE) #AC_FUNC_ERROR_AT_LINE
CHECK_FUNCTION_EXISTS(malloc HAVE_MALLOC) #AC_FUNC_MALLOC
CHECK_FUNCTION_EXISTS(mktime HAVE_MKTIME) #AC_FUNC_MKTIME
CHECK_FUNCTION_EXISTS(realloc HAVE_REALLOC) #AC_FUNC_REALLOC
CHECK_FUNCTION_EXISTS(strftime HAVE_STRFTIME) #AC_FUNC_STRFTIME
CHECK_FUNCTION_EXISTS(strtod HAVE_STRTOD) #AC_FUNC_STRTOD
CHECK_FUNCTION_EXISTS(vprintf HAVE_VPRINTF) #AC_FUNC_VPRINTF
CHECK_FUNCTION_EXISTS(alarm HAVE_ALARM)
CHECK_FUNCTION_EXISTS(atexit HAVE_ATEXIT)
CHECK_FUNCTION_EXISTS(ftime HAVE_FTIME)
CHECK_FUNCTION_EXISTS(getcwd HAVE_GETCWD)
CHECK_FUNCTION_EXISTS(gethostbyname HAVE_GETHOSTBYNAME)
CHECK_FUNCTION_EXISTS(gettimeofday HAVE_GETTIMEOFDAY)
CHECK_FUNCTION_EXISTS(inet_ntoa HAVE_INET_NTOA)
CHECK_FUNCTION_EXISTS(memset HAVE_MEMSET)
CHECK_FUNCTION_EXISTS(mkdir HAVE_MKDIR)
CHECK_FUNCTION_EXISTS(putenv HAVE_PUTENV)
CHECK_FUNCTION_EXISTS(select HAVE_SELECT)
CHECK_FUNCTION_EXISTS(setenv HAVE_SETENV)
CHECK_FUNCTION_EXISTS(socket HAVE_SOCKET)
CHECK_FUNCTION_EXISTS(strchr HAVE_STRCHR)
CHECK_FUNCTION_EXISTS(strcspn HAVE_STRCSPN)
CHECK_FUNCTION_EXISTS(strerror HAVE_STRERROR)
CHECK_FUNCTION_EXISTS(strpbrk HAVE_STRPBRK)
CHECK_FUNCTION_EXISTS(strrchr HAVE_STRRCHR)
CHECK_FUNCTION_EXISTS(strstr HAVE_STRSTR)
CHECK_FUNCTION_EXISTS(strtol HAVE_STRTOL)
CHECK_FUNCTION_EXISTS(tzset HAVE_TZSET)
CHECK_FUNCTION_EXISTS(gethrtime HAVE_GETHRTIME)
CHECK_FUNCTION_EXISTS(floor HAVE_FLOOR)
CHECK_FUNCTION_EXISTS(pow HAVE_POW)
CHECK_FUNCTION_EXISTS(sqrt HAVE_SQRT)
CHECK_FUNCTION_EXISTS(bindprocessor HAVE_BINDPROCESSOR)
CHECK_FUNCTION_EXISTS(thread_policy_set HAVE_THREAD_POLICY_SET)

CHECK_SYMBOL_EXISTS(sched_setaffinity sched.h HAVE_SCHED_SETAFFINITY)
CHECK_SYMBOL_EXISTS(sched_getaffinity sched.h HAVE_SCHED_GETAFFINITY)

CHECK_SYMBOL_EXISTS(THREAD_AFFINITY_POLICY mach/thread_policy.h HAVE_OSX_THREAD_AFFINITY_POLICY)
CHECK_SYMBOL_EXISTS(THREAD_AFFINITY_TAG_NULL mach/thread_policy.h HAVE_OSX_THREAD_AFFINITY_TAG_NULL)
IF (HAVE_OSX_THREAD_AFFINITY_POLICY AND HAVE_OSX_THREAD_AFFINITY_TAG_NULL)
    SET(HAVE_OSX_THREAD_AFFINITY 1)
ENDIF ()

# Check if _GNU_SOURCE is available.
if (NOT _GNU_SOURCE)
    CHECK_SYMBOL_EXISTS(__GNU_LIBRARY__ "features.h" _GNU_SOURCE)

    if (NOT _GNU_SOURCE)
        unset(_GNU_SOURCE CACHE)
        CHECK_SYMBOL_EXISTS(_GNU_SOURCE "features.h" _GNU_SOURCE)
    endif ()
endif ()

if (_GNU_SOURCE)
    add_definitions(-D_GNU_SOURCE)
endif ()

# Test for compiler builtin functions.
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/HAVE_CPU_SET_T.cc "
#include <sched.h>
int main() {
    cpu_set_t t;
    return 0;
}")
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/HAVE_DYN_PROC_AFFINITY.cc "
#include <sched.h>
int main() {
    cpu_set_t *cpu = CPU_ALLOC(2);
    return 0;
}")
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/HAVE_CPU_SET_MACROS.cc "
#include <sched.h>
int main() {
    cpu_set_t t;
    CPU_ZERO(&t);
    CPU_SET(1,&t);
    return 0;
}")
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/HAVE___SYNC_ADD_AND_FETCH.cc "
int main() {
    volatile unsigned int value = 0;
    __sync_add_and_fetch(&value, 1);
    return 0;
}")
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/HAVE___SYNC_BOOL_COMPARE_AND_SWAP.cc "
int main() {
    volatile unsigned int value = 0;
    __sync_bool_compare_and_swap(&value, value, 1);
    return 0;
}")
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/HAVE_GET_NPROCS.cc "
#include <sys/sysinfo.h>
int main() {
    get_nprocs();
}")
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/HAVE_ISFINITE.cc "
#include <math.h>
int main() {
    int result = isfinite(1.0);
}")
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/HAVE_PTHREAD_PRIO_INHERIT.cc "
#include <pthread.h>
int main() {
    int i = PTHREAD_PRIO_INHERIT;
}")

TRY_COMPILE(HAVE_CPU_SET_T ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/HAVE_CPU_SET_T.cc)
TRY_COMPILE(DYN_PROC_AFFINITY ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/HAVE_DYN_PROC_AFFINITY.cc)
TRY_COMPILE(HAVE_CPU_SET_MACROS ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/HAVE_CPU_SET_MACROS.cc)
TRY_COMPILE(HAVE___SYNC_ADD_AND_FETCH ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/HAVE___SYNC_ADD_AND_FETCH.cc)
TRY_COMPILE(HAVE___SYNC_BOOL_COMPARE_AND_SWAP ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/HAVE___SYNC_BOOL_COMPARE_AND_SWAP.cc)
TRY_COMPILE(HAVE_GET_NPROCS ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/HAVE_GET_NPROCS.cc)
TRY_COMPILE(HAVE_ISFINITE ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/HAVE_ISFINITE.cc)
TRY_COMPILE(HAVE_PTHREAD_PRIO_INHERIT ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/HAVE_PTHREAD_PRIO_INHERIT.cc)